<?php
/**
 * Converter to convert a value into a primitive
 * by trying to guess its type
 *
 * @package XJConf
 * @author  Stephan Schmidt <schst@xjconf.net>
 */
XJConfLoader::load('converters.ValueConverter',
                   'exceptions.ValueConversionException'
);
/**
 * Converter to convert a value into a primitive
 * by trying to guess its type
 *
 * @package XJConf
 */
class AutoPrimitiveValueConverter implements ValueConverter {

    /**
     * converts the given values into the given types
     *
     * @param   array  $values  list of values to convert
     * @return  mixed  the converted value
     * @throws  ValueConversionException
     */
    public function convertValue(Tag $tag, Definition $def) {
        $value = $tag->getData();

        if ($value === 'null') {
            return null;
        }
        if ($value === 'true') {
            return true;
        }
        if ($value === 'false') {
            return false;
        }

        if (preg_match('/^[+-]?[0-9]+$/', $value)) {
            settype($value, 'int');
        	return $value;
        }
        if (preg_match('/^[+-]?[0-9]*\.[0-9]+$/', $value)) {
        	settype($value, 'double');
        	return $value;
        }
        return $value;
    }

   /**
     * Returns the type of the converter
     *
     * @return  string
     */
    public function getType() {
        return null;
    }
}
?>